//
//  FirstScroller.m
//  MY Project For Sport
//
//  Created by dllo on 16/2/27.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "FirstScroller.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface FirstScroller ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scroller;
@property (nonatomic, retain)UIPageControl *page;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UIImageView *image;



@end


@implementation FirstScroller

-(void)dealloc {
    
    [_scroller release];
    [_page release];
    [_array release];
    [_image release];
    [_buttton release];
    [super dealloc];
    
    
}

-(instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self createScroller];
        [self createPage];
    }

    return self;

}


- (void)createScroller {
    
    // 第一次启动应用程序的图片数组
    self.array = @[@"80E5102A7F26A202707B652433E25329.jpg", @"FB0A9AD3B619BEC46FB15FF00C3B780D.jpg"].mutableCopy;
    
   // NSLog(@"%f", self.frame.size.height);
    // 将 scroller 添加到 view 上
    self.scroller = [[UIScrollView alloc] initWithFrame:self.frame];
    [self addSubview:self.scroller];
    [_scroller release];
    
    self.scroller.bounces = NO;
    // 制定代理人
    self.scroller.delegate = self;
    // 不显示水平和垂直滑动的横线
    self.scroller.showsHorizontalScrollIndicator = NO;
    self.scroller.showsVerticalScrollIndicator = NO;
    
    // 滑动的大小
    self.scroller.contentSize = CGSizeMake(self.frame.size.width * self.array.count, self.frame.size.height);
    // 开启滑动翻页效果
    self.scroller.pagingEnabled = YES;    
    // 遍历创建滑动的图片
    for (int i = 0; i < self.array.count; i++) {
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.array[i]]];
        
        image.frame = CGRectMake(i * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height);
        
        [self.scroller addSubview:image];
        [_image release];
        
    }
    
    self.buttton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttton.frame= CGRectMake(self.frame.size.width / 2 + self.frame.size.width - self.frame.size.width / 3, self.frame.size.height / 8 * 7,self.frame.size.width / 3 * 2, 30 * HEIGHTT);
   // self.buttton .backgroundColor = [UIColor redColor];
    [self.buttton setTitle:@"点击进入 app " forState:UIControlStateNormal];
    [self.scroller addSubview:self.buttton];
    
    
}

/**当 scorller 减速时调用此方法 关联 pagecontroller */

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    self.page.currentPage = scrollView.contentOffset.x / self.frame.size.width;
    
    
}

- (void)createPage {
    
    // 创建 pageContorller
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(50 * WEIGHTT, self.frame.size.height / 8 * 7, self.frame.size.width - 100 * WEIGHTT, self.frame.size.height / 8)];
    [self addSubview:self.page];
    [_page release];
    self.page.userInteractionEnabled = NO;
    self.page.backgroundColor = [UIColor clearColor];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    self.page.currentPageIndicatorTintColor = [UIColor redColor];
    
    self.page.numberOfPages = self.array.count;
    
    // 关联 scroller
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
}

/** 根据当前的页数计算 scroller 的偏移量 */
- (void)pageAction:(UIPageControl *) page {
    
    self.scroller.contentOffset = CGPointMake(page.currentPage * self.frame.size.width, 0);
    
    
}





/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
